<meta charset="utf-8">
(#) @SuppressLint on invalid element

!!! ERROR: @SuppressLint on invalid element
   This is an error.

Id
:   `LocalSuppress`
Summary
:   @SuppressLint on invalid element
Severity
:   Error
Category
:   Correctness
Platform
:   Any
Vendor
:   Android Open Source Project
Feedback
:   https://issuetracker.google.com/issues/new?component=192708
Since
:   Initial
Affects
:   Kotlin and Java files
Editing
:   This check runs on the fly in the IDE editor
Implementation
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/AnnotationDetector.kt)
Tests
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/AnnotationDetectorTest.kt)
Copyright Year
:   2012

The `@SuppressAnnotation` is used to suppress Lint warnings in Java
files. However, while many lint checks analyzes the Java source code,
where they can find annotations on (for example) local variables, some
checks are analyzing the `.class` files. And in class files, annotations
only appear on classes, fields and methods. Annotations placed on local
variables disappear. If you attempt to suppress a lint error for a
class-file based lint check, the suppress annotation not work. You must
move the annotation out to the surrounding method.

(##) Example

Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/test/pkg/WrongAnnotation.java:10:Error: The @SuppressLint annotation
cannot be used on a local variable with the lint check 'NewApi': move
out to the surrounding method [LocalSuppress]
    public static void foobar(View view, @SuppressLint("NewApi") int foo) { // $ Invalid: class-file check
                                         -----------------------
src/test/pkg/WrongAnnotation.java:11:Error: The @SuppressLint annotation
cannot be used on a local variable with the lint check 'NewApi': move
out to the surrounding method [LocalSuppress]
    @SuppressLint("NewApi") // Invalid
    -----------------------
src/test/pkg/WrongAnnotation.java:13:Error: The @SuppressLint annotation
cannot be used on a local variable with the lint check 'NewApi': move
out to the surrounding method [LocalSuppress]
    @SuppressLint({"SdCardPath", "NewApi"}) // Invalid: class-file based check on local variable
    ---------------------------------------
src/test/pkg/WrongAnnotation.java:15:Error: The @SuppressLint annotation
cannot be used on a local variable with the lint check 'NewApi': move
out to the surrounding method [LocalSuppress]
    @android.annotation.SuppressLint({"SdCardPath", "NewApi"}) // Invalid (FQN)
    ----------------------------------------------------------
src/test/pkg/WrongAnnotation.java:29:Error: The @SuppressLint annotation
cannot be used on a local variable with the lint check 'NewApi': move
out to the surrounding method [LocalSuppress]
    @SuppressLint("NewApi")
    -----------------------
src/test/pkg/WrongAnnotation.java:34:Error: The @SuppressLint annotation
cannot be used on a local variable with the lint check 'NewApi': move
out to the surrounding method [LocalSuppress]
    @SuppressLint("NewApi") // Invalid
    -----------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here is the source file referenced above:

`src/test/pkg/WrongAnnotation.java`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers

package test.pkg;

import android.annotation.SuppressLint;
import android.view.View;

public class WrongAnnotation {

    @SuppressLint("NewApi") // Valid: class-file check on method
    public static void foobar(View view, @SuppressLint("NewApi") int foo) { // $ Invalid: class-file check
        @SuppressLint("NewApi") // Invalid
        boolean a;
        @SuppressLint({"SdCardPath", "NewApi"}) // Invalid: class-file based check on local variable
        boolean b;
        @android.annotation.SuppressLint({"SdCardPath", "NewApi"}) // Invalid (FQN)
        boolean c;
        @SuppressLint("SdCardPath") // Valid: AST-based check
        boolean d;
    }

    @SuppressLint("NewApi")
    private int field1;

    @SuppressLint("NewApi")
    private int field2 = 5;

    static {
        // Local variable outside method: invalid
        @SuppressLint("NewApi")
        int localvar = 5;
    }

    private static void test() {
        @SuppressLint("NewApi") // Invalid
        int a = View.MEASURED_STATE_MASK;
    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also visit the
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/AnnotationDetectorTest.kt)
for the unit tests for this check to see additional scenarios.

The above example was automatically extracted from the first unit test
found for this lint check, `AnnotationDetector.testBasic`.
To report a problem with this extracted sample, visit
https://issuetracker.google.com/issues/new?component=192708.

(##) Suppressing

You can suppress false positives using one of the following mechanisms:

* Using a suppression annotation like this on the enclosing
  element:

  ```kt
  // Kotlin
  @Suppress("LocalSuppress")
  fun method() {
     problematicStatement()
  }
  ```

  or

  ```java
  // Java
  @SuppressWarnings("LocalSuppress")
  void method() {
     problematicStatement();
  }
  ```

* Using a suppression comment like this on the line above:

  ```kt
  //noinspection LocalSuppress
  problematicStatement()
  ```

* Using a special `lint.xml` file in the source tree which turns off
  the check in that folder and any sub folder. A simple file might look
  like this:
  ```xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;lint&gt;
      &lt;issue id="LocalSuppress" severity="ignore" /&gt;
  &lt;/lint&gt;
  ```
  Instead of `ignore` you can also change the severity here, for
  example from `error` to `warning`. You can find additional
  documentation on how to filter issues by path, regular expression and
  so on
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).

* In Gradle projects, using the DSL syntax to configure lint. For
  example, you can use something like
  ```gradle
  lintOptions {
      disable 'LocalSuppress'
  }
  ```
  In Android projects this should be nested inside an `android { }`
  block.

* For manual invocations of `lint`, using the `--ignore` flag:
  ```
  $ lint --ignore LocalSuppress ...`
  ```

* Last, but not least, using baselines, as discussed
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).

<!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://morgan3d.github.io/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>